package com.visionork.android.tai.model.adapter;

import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import com.visionork.android.tai.model.Episode;

public class EpisodeAdapter extends TaiAdapterBase {
	private static final String PREFIX_1 = "http://sugoitw.sugoideas.com";
	private static final String PREFIX_2 = "http://sugoideas.com";
	
	public EpisodeAdapter(String urlLink) {
		super(urlLink);		
	}

	@Override
	public ArrayList<?> getData() {
		ArrayList<Episode> episodes = new ArrayList<Episode>();
		try {
			Document doc = Jsoup.parse(new URL(urlLink), TIME_OUT);
			Elements elms = doc.getElementsByTag("a");

			for (Element elm : elms) {				
				String href = elm.attr("href");
				if (elm.hasAttr("target")
						&& elm.attr("target").equals("_blank")
						&& elm.hasText()
						&& (href.startsWith(PREFIX_1) || href
								.startsWith(PREFIX_2))) {
					String title = elm.text();
					if (title != null && title.trim().length() > 0) {
						Episode episode = new Episode();
						episode.setTitle(title);
						episode.setUrlLink(href);
						episodes.add(episode);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return episodes;
	}
}
